/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.core.editpart;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.NonResizableEditPolicy;
import org.eclipse.uml2.uml.Extension;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.figures.StringFigure;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * 
 * 
 * @author Patrick Tessier
 * 26/07/2006
 * This is the EditPart supporting the edition of the required property of an extenssion
 */
public class ExtensionRequiredEditPart extends LinkNameDisplayEditPart{


	/**
	 * 
	 */
	protected int channel=14;
//	protected RequiredAdapter requiredpropertyListener= new RequiredAdapter();



	/**
 * 
 * 
 * @param modelManager 
 * @param graphnode 
 */
public ExtensionRequiredEditPart(GraphNode graphnode, ModelManager modelManager) {
		super(graphnode,modelManager);

	}


	/**
	 * 
	 * 
	 * @param element 
	 * @param modelManager 
	 * @param graphnode 
	 */
	public ExtensionRequiredEditPart(GraphNode graphnode,org.eclipse.uml2.uml.Extension element,ModelManager modelManager) {
		super(graphnode,element,modelManager);


	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.NameDisplayEditPart#createEditPolicies()
	 */
	/**
	 * 
	 */
	@Override
	protected void createEditPolicies(){
		installEditPolicy(EditPolicy.PRIMARY_DRAG_ROLE, new NonResizableEditPolicy());
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.NameDisplayEditPart#createFigure()
	 */
	/**
	 * 
	 * 
	 * @return 
	 */
	@Override
	protected StringFigure createFigure() {
		return new StringFigure("{required}");
	}

	/* (non-Javadoc)
	 * @see com.cea.papyrus.core.common.editpart.NameDisplayEditPart#refreshVisuals()
	 */
	/**
	 * 
	 */
	@Override
	protected void refreshVisuals() {
		StringFigure f = (StringFigure)getFigure();
		Dimension size = getGraphNode().getSize();
		Point p = getGraphNode().getPosition();
		Rectangle r = new Rectangle(p ,size);

		if(((Extension)element).isRequired()){
			this.getFigure().setVisible(true);
		}
		else {
			this.getFigure().setVisible(false);
		}

//		if(((Extension)element).isRequired()){
//		((StringFigure) this.getFigure()).setText("{required}");
//		}
//		else {
//		((StringFigure) this.getFigure()).setText("");
//		}
	}



//	/* (non-Javadoc)
//	* @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#activate()
//	*/
//	@Override
//	public void activate()
//	{
//	((Notifier)(Property)((Extension)element).getOwnedEnds().get(0)).eAdapters().add(requiredpropertyListener);
//	if (((Property)((Extension)element).getOwnedEnds().get(0)).getLowerValue()!=null) {
//	((Notifier)((Property)((Extension)element).getOwnedEnds().get(0)).getLowerValue()).eAdapters().add(requiredpropertyListener);
//	}
//	super.activate();
//	}


//	/* (non-Javadoc)
//	* @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#deactivate()
//	*/
//	@Override
//	public void deactivate()
//	{
//	super.deactivate();
//	((Notifier)(Property)((Extension)element).getOwnedEnds().get(0)).eAdapters().remove(requiredpropertyListener);
//	if (((Property)((Extension)element).getOwnedEnds().get(0)).getLowerValue()!=null){
//	((Notifier)((Property)((Extension)element).getOwnedEnds().get(0)).getLowerValue()).eAdapters().remove(requiredpropertyListener);
//	}
//	}

//	class RequiredAdapter implements Adapter {
//	private Notifier _target;

//	/**
//	* @see org.eclipse.emf.common.notify.Adapter#notifyChanged(org.eclipse.emf.common.notify.Notification)
//	*/
//	public void notifyChanged(Notification notification) {
//	Debug.debug(this,"ExtensionRequired._adapter notifyChanged: " + notification,channel);
//	int type = notification.getEventType();
//	refreshVisuals();

//	// activate();
//	}
//	/**
//	* @see org.eclipse.emf.common.notify.Adapter#getTarget()
//	*/
//	public Notifier getTarget() {
//	return _target;
//	}

//	/**
//	* @see org.eclipse.emf.common.notify.Adapter#setTarget(org.eclipse.emf.common.notify.Notifier)
//	*/
//	public void setTarget(Notifier newTarget) {
//	_target = newTarget;
//	}

//	/**
//	* @see org.eclipse.emf.common.notify.Adapter#isAdapterForType(java.lang.Object)
//	*/
//	public boolean isAdapterForType(Object type) {
//	return type.equals(getModel().getClass());
//	}
//	}
}
